var editUserUrl = null;
var editUserMethod = null;
var editMenuUrl = null;
var editMenuMethod = null;
var idsArr = [];
var rowDataArr = [];
var userIdsArr=[];
var userRowDataArr = [];
var roleIdData;


$(function() {
    $("#dg_role_accredit").datagrid({
                                   url: ctx + "/sys/roleAccredit/loadRole",
                                   method: "get",
                                   onSelect: onSelectDgRoleAccredit
                               });
    $("#dg_user_accredit").datagrid({
                                   url:ctx + "/sys/roleAccredit/loadUser",
                                   method:"get"
                               })
    $("#dg_menu_accredit").datagrid({
                                 url:ctx + "/sys/roleAccredit/loadMenu",
                                 method:"get"
                      })
})

function onSelectDgRoleAccredit(index,row) {
    var roleId = row.id;
    $("#dg_user_accredit").datagrid({
                                   url: ctx + "/sys/roleAccredit/role_user/" + roleId,
                                   method: "get",
                               });
    $('#dg_menu_accredit').datagrid({
                                  url: ctx + "/sys/roleAccredit/role_menu/" + roleId,
                                  method: "get"
                      });

    $('#btnUserListEdit').bind('click', function () {
        editUserUrl = ctx + 'sys/user/' + roleId;
        editUserMethod = "put";
        $('#dl_user_add').dialog('open');
        //把原来的树形数据给清空
        $('#roleUserTab').tree('loadData', {});
        //重新加载右侧的菜单树
        init_roleUserTab(roleId);
    });
    $('#btnMenuListEdit').bind('click', function () {
        editMenuUrl = ctx + 'sys/menu/' + roleId;
        editMenuMethod = "put";
        $('#dl_menu_add').dialog('open');
        //把原来的树形数据给清空
        $('#roleMenuTab').tree('loadData', {});
        //重新加载右侧的菜单树
        init_roleMenuTab(roleId);
    });
}

var init_roleUserTab = function (roleId) {
    $('#roleUserTab').tree({
                               url: ctx + "/sys/limit/role_user_menu/" + roleId,//后台返回属形数据的url
                               method: 'get',
                               checkbox: true
                           });
    roleIdData = roleId;
}
var init_roleMenuTab = function (roleId) {
    $('#roleMenuTab').tree({
                               url: ctx + "/sys/limit/role_menu_menu/" + roleId,//后台返回属形数据的url
                               method: 'get',
                               checkbox: true
                           });
    roleIdData = roleId;
}
var findChildrenIdsrole = function (node, idArr) {
    var ids = node.id;
    idArr.push(ids);
    var children = node.children;
    if (!node.children) {
        return;
    }
    for (var i = 0; i < children.length; i++) {
        findChildrenIdsrole(children[i], idArr);
    }
}



var submitUserAcc = function () {
    userRowDataArr = $('#roleUserTab').tree('getChecked');
    for (var i = 0; i < userRowDataArr.length; i++) {
        userIdsArr[i] = userRowDataArr[i].id;
    }
    $.ajax({
               url: ctx + "/sys/roleAccredit/saveUser/" + roleIdData + "/" + userIdsArr,//后台返回属形数据的url
               type: 'put',
               dataType:'text',
               success:function (data) {
                   $('#dl_user_add').dialog('close');
                   $('#dg_user_accredit').datagrid('reload');
               }
           })


}
function submitMenuAcc() {
    rowDataArr = $('#roleMenuTab').tree('getChecked');
    for (var i = 0; i < rowDataArr.length; i++) {
        idsArr[i] = rowDataArr[i].id;
    }
    $.ajax({
               url: ctx + "/sys/roleAccredit/saveMenu/" + roleIdData + "/" + idsArr,//后台返回属形数据的url
               type: 'put',
               dataType:'text',
               success:function (data) {
                   $('#dl_menu_add').dialog('close');
                   $('#dg_menu_accredit').treegrid('reload');
               }
           })
}
function closeMenuEditAcc() {
    $('#dl_menu_add').dialog('close');
}
var closeUserEditAcc = function () {
    $('#dl_user_add').dialog('close');
}








